package com.ruoyi.web.qualityController;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import java.util.List;
import java.util.Arrays;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.quality.QualityDefectType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.service.IQualityDefectTypeService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 质量缺陷类型Controller
 *
 * @author ruoyi
 * @date 2023-03-07
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/quality/type" )
@Api(tags = "质量缺陷类型")
public class QualityDefectTypeController extends BaseController {

    private final IQualityDefectTypeService iQualityDefectTypeService;

    /**
     * 查询质量缺陷类型列表
     */
    //@PreAuthorize("@ss.hasPermi('quality:type:list')")
    @ApiOperation(value = "查询质量缺陷类型列表")
    @GetMapping("/list")
    public TableDataInfo list(QualityDefectType qualityDefectType)
    {
        startPage();
        LambdaQueryWrapper<QualityDefectType> lqw = new LambdaQueryWrapper<QualityDefectType>();
        if (StringUtils.isNotBlank(qualityDefectType.getCode())){
            lqw.like(QualityDefectType::getCode ,qualityDefectType.getCode());
        }
        if (StringUtils.isNotBlank(qualityDefectType.getName())){
            lqw.like(QualityDefectType::getName ,qualityDefectType.getName());
        }
        if (StringUtils.isNotBlank(qualityDefectType.getDescription())){
            lqw.like(QualityDefectType::getDescription ,qualityDefectType.getDescription());
        }
        if (qualityDefectType.getId() != null){
            lqw.eq(QualityDefectType::getId ,qualityDefectType.getId());
        }
        List<QualityDefectType> list = iQualityDefectTypeService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 导出质量缺陷类型列表
     */
    //@PreAuthorize("@ss.hasPermi('quality:type:export')" )
    @Log(title = "质量缺陷类型" , businessType = BusinessType.EXPORT)
    @ApiOperation(value = "导出质量缺陷类型列表")
    @GetMapping("/export" )
    public AjaxResult export(QualityDefectType qualityDefectType) {
        LambdaQueryWrapper<QualityDefectType> lqw = new LambdaQueryWrapper<QualityDefectType>(qualityDefectType);
        List<QualityDefectType> list = iQualityDefectTypeService.list(lqw);
        ExcelUtil<QualityDefectType> util = new ExcelUtil<QualityDefectType>(QualityDefectType. class);
        return util.exportExcel(list, "type" );
    }

    /**
     * 获取质量缺陷类型详细信息
     */
    //@PreAuthorize("@ss.hasPermi('quality:type:query')" )
    @GetMapping(value = "/{id}" )
    @ApiOperation(value = "获取质量缺陷类型详细信息")
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iQualityDefectTypeService.getById(id));
    }

    /**
     * 新增质量缺陷类型
     */
    //@PreAuthorize("@ss.hasPermi('quality:type:add')" )
    @Log(title = "质量缺陷类型" , businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation(value = "新增质量缺陷类型")
    public AjaxResult add(@RequestBody QualityDefectType qualityDefectType) {
       iQualityDefectTypeService.checkUnique("quality_defect_type",qualityDefectType.getCode(),null);
        return toAjax(iQualityDefectTypeService.save(qualityDefectType) ? 1 : 0);
    }

    /**
     * 修改质量缺陷类型
     */
    //@PreAuthorize("@ss.hasPermi('quality:type:edit')" )
    @Log(title = "质量缺陷类型" , businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation(value = "修改质量缺陷类型")
    public AjaxResult edit(@RequestBody QualityDefectType qualityDefectType) {
        iQualityDefectTypeService.checkUnique("quality_defect_type",qualityDefectType.getCode(),qualityDefectType.getId());
        return toAjax(iQualityDefectTypeService.updateById(qualityDefectType) ? 1 : 0);
    }

    /**
     * 删除质量缺陷类型
     */
    //@PreAuthorize("@ss.hasPermi('quality:type:remove')" )
    @ApiOperation(value = "删除质量缺陷类型")
    @Log(title = "质量缺陷类型" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iQualityDefectTypeService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }


}
